package per.chenyl.framework.persistence.entity.aop;//package per.chenyl.framework.persistence.entity.aop;
//
//import per.chenyl.framework.persistence.entity.exception.FieldValidateException;
//import per.chenyl.framework.persistence.entity.anno.*;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Component;
//
//import java.lang.reflect.Field;
//import java.lang.reflect.Method;
//import java.util.Objects;
//
///**
// * Created by IntelliJ IDEA
// *
// * @author weiwenjun
// * @date 2018/9/12
// */
//@Aspect
//@Component
//public class EntityValidateAspect {
//    private Logger logger = LoggerFactory.getLogger(EntityValidateAspect.class);
//
//    @Pointcut("@annotation(per.chenyl.framework.persistence.entity.anno.Validator)")
//    public void operationLog() {
//    }
//
//
//    @Before("operationLog()")
//    public void doBeforeAdvice(JoinPoint joinPoint) {
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
//        logger.info("方法{}进行参数校验", method.getName());
//        if (method.isAnnotationPresent(Validator.class)) {
//            Object[] args = joinPoint.getArgs();
//            for (Object arg : args) {
//                for (Class<?> cl = arg.getClass(); cl != Object.class &&
//                        (cl.isAnnotationPresent(Entity.class) || cl.isAnnotationPresent(BaseEntity.class)); cl = cl.getSuperclass()) {
//                    Field[] fields = cl.getDeclaredFields();
//                    for (Field field : fields) {
//                        field.setAccessible(true);
//                        Object o = null;
//                        try {
//                            o = field.get(arg);
//                        } catch (IllegalAccessException e) {
//                            e.printStackTrace();
//                        }
//                        if (field.isAnnotationPresent(Notnull.class) && Objects.isNull(o)) {
//                            Notnull notnull = field.getAnnotation(Notnull.class);
//                            throw new NullPointerException(notnull.message());
//                        }
//                        if (field.isAnnotationPresent(NotEmpty.class) && (Objects.isNull(o) || String.valueOf(o).length() == 0)) {
//                            NotEmpty notEmpty = field.getAnnotation(NotEmpty.class);
//                            throw new NullPointerException(notEmpty.message());
//                        }
//                        if (field.isAnnotationPresent(Pattern.class)) {
//                            Pattern pattern = field.getAnnotation(Pattern.class);
//                            if (!String.valueOf(o).matches(pattern.regex())) {
//                                throw new FieldValidateException(pattern.message());
//                            }
//                        }
//                        if (field.isAnnotationPresent(MaxLength.class)) {
//                            MaxLength maxAnno = field.getAnnotation(MaxLength.class);
//                            if (String.valueOf(o).length() > maxAnno.value()) {
//                                throw new FieldValidateException(maxAnno.message());
//                            }
//                        }
//                    }
//                }
//
//            }
//        }
//    }
//
//
//}